10. Exercise: Methods (Part 2)

Methods Exercise (Part 2)

multiplyNumbers

Task Description:

Now, see if you can write out a method on your own, remembering the syntax from the previous exercise.

You method should take two integers as input, multiply them together, and return the result.

To multiply numbers in Java, we use the multiplication operating, which is simply an asterisk *.

Task List:

Task Feedback:

Nice work!

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    export PATH=/data/jdk-15.0.1/bin:$PATH
    export JAVA_HOME=/data/jdk-15.0.1/bin

  • Solution

    ND079 C1 L1 A10 Solution 2

    public class MethodExercise{
    
        public static void main(String[] args){
            System.out.println("The first product is: " + MethodExercise.multiplyNumbers(7,7));
            System.out.println("The second product is: " + MethodExercise.multiplyNumbers(2,3));
        }
    
        public static int multiplyNumbers(int num1, int num2) {
            return num1 * num2;
        }
    
    }